Skip Headers

Oracle® Streams Advanced Queuing User's Guide and Reference 10g Release 2 (10.2) Part Number B14257-01 |
Home | Book List | Contents | Index | Master Index | Feedback

Previous | Next
| View PDF

# D Pro*C/C++ Examples

This appendix presents examples using the Oracle Streams Advanced Queuing (AQ) Pro*C/C++ interface. You must set up data structures similar to the following for certain examples to work.

    $ cat >> message.typ
    case=lower
    type aq.message_typ
    $
    $ ott userid=aq/aq intyp=message.typ outtyp=message_o.typ \  code=c hfile=demo.h
    $
    $ proc intyp=message_o.typ iname=program name \
    config=config file SQLCHECK=SEMANTICS userid=aq/aq

    $ cat >> message.typ
    case=lower
    type aq.message_typ
    $
    $ ott userid=aq/aq intyp=message.typ outtyp=message_o.typ \  code=c hfile=demo.h
    $
    $ proc intyp=message_o.typ iname=program name \
    config=config file SQLCHECK=SEMANTICS userid=aq/aq

This appendix contains these topics:

- Enqueuing and Dequeuing Object Type Messages Using Pro*C/C++

- Enqueuing and Dequeuing RAW Type Messages Using Pro*C/C++

- Enqueuing and Dequeuing Messages by Correlaton and Message ID Using Pro*C/C++

## D.1 Enqueuing and Dequeuing Object Type Messages Using Pro*C/C++

This section provides an example that enqueues and dequeues object type messages using Pro*C/C++.

Example D-1 Enqueuing and Dequeuing Object Type Messages (Pro*C/C++)

    #include  <stdio.h>
    #include  <string.h>
    #include  <sqlca.h>
    #include  <sql2oci.h>
    /* The header file generated by processing
    object type 'aq.Message_typ': */
    #include  "pceg.h"

    void sql_error(msg)
    char *msg;
    {
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    printf("%s\n", msg);
    printf("\n% .800s \n", sqlca.sqlerrm.sqlerrmc);
    EXEC SQL ROLLBACK WORK RELEASE;
    exit(1);
    }

    main()
    {
    Message_typ     *message = (Message_typ*)0;  /* payload */
    message_type_ind *imsg;              /*payload indicator*/
    char             user[60]="aq/AQ";  /* user logon password */
    char             subject[30];  /* components of the */
    char             txt[80];      /* payload type */

    /* ENQUEUE and DEQUEUE to an OBJECT QUEUE */

    /*  Connect to database: */
    EXEC SQL CONNECT :user;

    /* On an oracle error print the error number :*/
    EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle Error :");

    /* Allocate memory for the host variable from the object cache : */
    EXEC SQL ALLOCATE :message;

    /* ENQUEUE */

    strcpy(subject, "NORMAL ENQUEUE");
    strcpy(txt, "The Enqueue was done through PLSQL embedded in PROC");

    /* Initialize the components of message : */
    EXEC SQL OBJECT SET subject, text OF  :message TO :subject, :txt;

    /* Embedded PLSQL call to the AQ enqueue procedure : */
    EXEC SQL EXECUTE
    DECLARE
    message_properties   dbms_aq.message_properties_t;
    enqueue_options      dbms_aq.enqueue_options_t;
    msgid                RAW(16);
    BEGIN
    /* Bind the host variable 'message' to the payload: */
    dbms_aq.enqueue(queue_name => 'msg_queue',
    message_properties => message_properties,
    enqueue_options => enqueue_options,
    payload => :message:imsg,    /* indicator must be specified */
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work */
    EXEC SQL COMMIT;

    printf("Enqueued Message \n");
    printf("Subject  :%s\n",subject);
    printf("Text     :%s\n",txt);

    /* Dequeue */

    /* Embedded PLSQL call to the AQ dequeue procedure : */
    EXEC SQL EXECUTE
    DECLARE
    message_properties  dbms_aq.message_properties_t;
    dequeue_options     dbms_aq.dequeue_options_t;
    msgid               RAW(16);
    BEGIN
    /* Return the payload into the host variable 'message':  */
    dbms_aq.dequeue(queue_name => 'msg_queue',
    message_properties => message_properties,
    dequeue_options => dequeue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
     /* Commit work :*/
    EXEC SQL COMMIT;

    /* Extract the components of message: */
    EXEC SQL OBJECT GET SUBJECT,TEXT FROM :message INTO :subject,:txt;

    printf("Dequeued Message \n");
    printf("Subject  :%s\n",subject);
    printf("Text     :%s\n",txt);
    }

## D.2 Enqueuing and Dequeuing RAW Type Messages Using Pro*C/C++

This section provides an example that enqueues and dequeues RAW type messages using Pro*C/C++.

Example D-2 Enqueuing and Dequeuing RAW Type Messages (Pro*C/C++ )

    #include  <stdio.h>
    #include  <string.h>
    #include  <sqlca.h>
    #include  <sql2oci.h>

    void sql_error(msg)
    char *msg;
    {
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    printf("%s\n", msg);
    printf("\n% .800s \n", sqlca.sqlerrm.sqlerrmc);
    EXEC SQL ROLLBACK WORK RELEASE;
    exit(1);
    }

    main()
    {
    OCIEnv         *oeh;   /* OCI Env handle */
    OCIError       *err;   /* OCI Err handle */
    OCIRaw         *message= (OCIRaw*)0;   /* payload */
    ub1            message_txt[100];   /* data for payload */
    char           user[60]="aq/AQ";   /* user logon password */
    int            status;   /* returns status of the OCI call */

    /* Enqueue and dequeue to a RAW queue */

    /* Connect to database: */
    EXEC SQL CONNECT :user;

    /* On an oracle error print the error number: */
    EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle Error :");

    /* Get the OCI Env handle: */
    if (SQLEnvGet(SQL_SINGLE_RCTX, &oeh) != OCI_SUCCESS)
    {
    printf(" error in SQLEnvGet \n");
    exit(1);
    }
    /* Get the OCI Error handle: */
    if (status = OCIHandleAlloc((dvoid *)oeh, (dvoid **)&err,
    (ub4)OCI_HTYPE_ERROR, (ub4)0, (dvoid **)0))
    {
    printf(" error in OCIHandleAlloc %d \n", status);
    exit(1);
    }

    /* Enqueue */
    /* The bytes to be put into the raw payload:*/
    strcpy(message_txt, "Enqueue to a Raw payload queue ");

    /* Assign bytes to the OCIRaw pointer :
    Memory must be allocated explicitly to OCIRaw*: */
    if (status=OCIRawAssignBytes(oeh, err, message_txt, 100,
     &message))
    {
    printf(" error in  OCIRawAssignBytes  %d \n", status);
    exit(1);
    }

    /*  Embedded PLSQL call to the AQ enqueue procedure : */
    EXEC SQL EXECUTE
    DECLARE
    message_properties   dbms_aq.message_properties_t;
    enqueue_options      dbms_aq.enqueue_options_t;
    msgid                RAW(16);
    BEGIN
    /* Bind the host variable message to the raw payload: */
    dbms_aq.enqueue(queue_name => 'raw_msg_queue',
    message_properties => message_properties,
    enqueue_options => enqueue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work: */
    EXEC SQL COMMIT;

    /* Dequeue */
    /*  Embedded PLSQL call to the AQ dequeue procedure :*/
    EXEC SQL EXECUTE
    DECLARE
    message_properties  dbms_aq.message_properties_t;
    dequeue_options     dbms_aq.dequeue_options_t;
    msgid               RAW(16);
    BEGIN
    /* Return the raw payload into the host variable 'message':*/
    dbms_aq.dequeue(queue_name => 'raw_msg_queue',
    message_properties => message_properties,
    dequeue_options => dequeue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work: */
    EXEC SQL COMMIT;
    }

## D.3 Enqueuing and Dequeuing Messages by Correlaton and Message ID Using Pro*C/C++

This section provides an example that enqueues and dequeues messages by correlation and message ID.

Example D-3 Enqueuing and Dequeuing by Correlation and Message ID (Pro*C/C++)

    #include  <stdio.h>
    #include  <string.h>
    #include  <sqlca.h>
    #include  <sql2oci.h>
    /* The header file generated by processing
    object type 'aq.Message_typ': */
    #include  "pceg.h"

    void sql_error(msg)
    char *msg;
    {
    EXEC SQL WHENEVER SQLERROR CONTINUE;
    printf("%s\n", msg);
    printf("\n% .800s \n", sqlca.sqlerrm.sqlerrmc);
    EXEC SQL ROLLBACK WORK RELEASE;
    exit(1);
    }

    main()
    {
    OCIEnv            *oeh;  /* OCI Env Handle */
    OCIError          *err;  /* OCI Error Handle */
    Message_typ      *message = (Message_typ*)0; /* queue  payload */
    message_type_ind *imsg;              /*payload indicator*/
    OCIRaw            *msgid = (OCIRaw*)0;  /* message id */
    ub1               msgmem[16]="";  /* memory for msgid */
    char              user[60]="aq/AQ";  /* user login password */
    char              subject[30];  /* components of */
    char              txt[80];  /* Message_typ */
    char              correlation1[30];  /* message correlation  */
    char              correlation2[30];
    int               status;  /* code returned by the OCI calls */

    /* Dequeue by correlation and msgid */

    /* Connect to the database: */
    EXEC SQL CONNECT :user;
    EXEC SQL WHENEVER SQLERROR DO sql_error("Oracle Error :");

    /* Allocate space in the object cache for the host variable: */
    EXEC SQL ALLOCATE :message;

    /* Get the OCI Env handle: */
    if (SQLEnvGet(SQL_SINGLE_RCTX, &oeh) != OCI_SUCCESS)
    {
     printf(" error in SQLEnvGet \n");
     exit(1);
    }
    /* Get the OCI Error handle: */
    if (status = OCIHandleAlloc((dvoid *)oeh, (dvoid **)&err,
    (ub4)OCI_HTYPE_ERROR, (ub4)0, (dvoid **)0))
    {
    printf(" error in OCIHandleAlloc %d \n", status);
    exit(1);
    }

    /* Assign memory for msgid:
    Memory must be allocated explicitly to OCIRaw*: */
    if (status=OCIRawAssignBytes(oeh, err, msgmem, 16, &msgid))
    {
    printf(" error in  OCIRawAssignBytes  %d \n", status);
    exit(1);
    }

    /* First enqueue */

    strcpy(correlation1, "1st message");
    strcpy(subject, "NORMAL ENQUEUE1");
    strcpy(txt, "The Enqueue was done through PLSQL embedded in PROC");

    /* Initialize the components of message: */
    EXEC SQL OJECT SET subject, text OF :message TO :subject, :txt;

    /* Embedded PLSQL call to the AQ enqueue procedure: */
    EXEC SQL EXECUTE
    DECLARE
    message_properties   dbms_aq.message_properties_t;
    enqueue_options      dbms_aq.enqueue_options_t;
    BEGIN
    /* Bind the host variable 'correlation1': to message correlation*/
    message_properties.correlation := :correlation1;

    /* Bind the host variable 'message' to payload and
     return message ID into host variable 'msgid': */
    dbms_aq.enqueue(queue_name => 'msg_queue',
    message_properties => message_properties,
    enqueue_options => enqueue_options,
    payload => :message:imsg,    /* indicator must be specified */
    msgid => :msgid);
    END;
    END-EXEC;
    /* Commit work: */
    EXEC SQL COMMIT;

    printf("Enqueued Message \n");
    printf("Subject  :%s\n",subject);
    printf("Text     :%s\n",txt);

    /* Second enqueue */

    strcpy(correlation2, "2nd message");
    strcpy(subject, "NORMAL ENQUEUE2");
    strcpy(txt, "The Enqueue was done through PLSQL embedded in PROC");

    /* Initialize the components of message: */
    EXEC SQL OBJECT SET subject, text OF :messsage TO :subject,:txt;

    /* Embedded PLSQL call to the AQ enqueue procedure: */
    EXEC SQL EXECUTE
    DECLARE
    message_properties   dbms_aq.message_properties_t;
    enqueue_options      dbms_aq.enqueue_options_t;
    msgid                RAW(16);
    BEGIN
    /* Bind the host variable 'correlation2':  to message correlaiton */
    message_properties.correlation := :correlation2;

    /* Bind the host variable 'message': to payload */
    dbms_aq.enqueue(queue_name => 'msg_queue',
    message_properties => message_properties,
    enqueue_options => enqueue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work: */
    EXEC SQL COMMIT;
    printf("Enqueued Message \n");
    printf("Subject  :%s\n",subject);
    printf("Text     :%s\n",txt);

    /* First dequeue - by  correlation */

    EXEC SQL EXECUTE
    DECLARE
    message_properties  dbms_aq.message_properties_t;
    dequeue_options     dbms_aq.dequeue_options_t;
    msgid               RAW(16);
    BEGIN
    /* Dequeue by  correlation in host variable 'correlation2': */
    dequeue_options.correlation := :correlation2;

    /* Return the payload into host variable 'message': */
    dbms_aq.dequeue(queue_name => 'msg_queue',
    message_properties => message_properties,
    dequeue_options => dequeue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work : */
    EXEC SQL COMMIT;

    /* Extract the values of the components of message: */
    EXEC SQL OBJECT GET subject, text FROM :message INTO :subject,:txt;

    printf("Dequeued Message \n");
    printf("Subject  :%s\n",subject);
    printf("Text     :%s\n",txt);

    /* SECOND DEQUEUE - by MSGID  */

    EXEC SQL EXECUTE
    DECLARE
    message_properties  dbms_aq.message_properties_t;
    dequeue_options     dbms_aq.dequeue_options_t;
    msgid               RAW(16);
    BEGIN
    /* Dequeue by msgid in host variable 'msgid': */
    dequeue_options.msgid := :msgid;

    /* Return the payload into host variable 'message':  */
    dbms_aq.dequeue(queue_name => 'msg_queue',
    message_properties => message_properties,
    dequeue_options => dequeue_options,
    payload => :message,
    msgid => msgid);
    END;
    END-EXEC;
    /* Commit work: */
    EXEC SQL COMMIT;
    }